package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.VipDAO;
import com.wanmait.hotelManage.vo.Vip;

@WebServlet("/manage/VipServlet")
public class VipServlet extends HttpServlet {

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if("init".equals(action)) {
			this.init(request,response);
		}else if("updateInit".equals(action)) {
			this.updateInit(request, response);
		}else if("update".equals(action)) {
			this.update(request, response);
		}else if("delete".equals(action)) {
			this.delete(request, response);
		}else if("insert".equals(action)) {
			this.insert(request, response);
		}
	}
	//添加
	public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String title = request.getParameter("title");
		String price = request.getParameter("price");
		String discount = request.getParameter("discount");
		Vip vip=new Vip();
		vip.setDiscount(Double.parseDouble(discount));
		vip.setPrice(Double.parseDouble(price));
		vip.setTitle(title);
		VipDAO.getVipDao().insert(vip);
		response.sendRedirect(request.getContextPath()+"/manage/VipServlet?action=init");
	}
	//删除
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		VipDAO.getVipDao().delete(Integer.parseInt(id));
	}
	//编辑
	public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String title = request.getParameter("title");
		String price = request.getParameter("price");
		String discount = request.getParameter("discount");
		String id = request.getParameter("vipId");
		Vip vip=new Vip();
		vip.setDiscount(Double.parseDouble(discount));
		vip.setPrice(Double.parseDouble(price));
		vip.setTitle(title);
		vip.setId(Integer.parseInt(id));
		
		VipDAO.getVipDao().update(vip);
		response.sendRedirect(request.getContextPath()+"/manage/VipServlet?action=init");
	}
	//编辑初始化
	public void updateInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		Vip vip=VipDAO.getVipDao().findVipById(Integer.parseInt(id));
		request.setAttribute("vip", vip);
		request.getRequestDispatcher("/manage/vip/update.jsp").forward(request, response);
	}
	//初始化
	public void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Vip> vips=VipDAO.getVipDao().list();
		request.setAttribute("vips", vips);
		request.getRequestDispatcher("/manage/vip/list.jsp").forward(request,response);
	}
}

